package com.shop.shopserver.controller.user;

import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.shop.shopcommon.result.Result;
import com.shop.shoppojo.dto.AiChatDTO;
import com.shop.shoppojo.entity.MedicalRecord;
import com.shop.shoppojo.vo.AiChatVO;
import com.shop.shopserver.service.AiChatService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.shop.shopcommon.constant.MessageConstant.SUCCESS;

@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "ai模块")
public class AIChatController {
    @Autowired
    private AiChatService aiChatService;
    /**
     * 保存用户健康信息
     * @return
     */
    @PostMapping("/saveMedicalRecord")
    public Result<String> saveMedicalRecord(@RequestBody MedicalRecord medicalRecord){
        // 保存用户健康信息
        log.info("保存用户健康信息：{}", medicalRecord);
        aiChatService.saveMedicalRecord(medicalRecord);
        return Result.success(null,SUCCESS);
    }

    /**
     * 获取用户健康信息
     * @return
     */
    @GetMapping("/getMedicalRecord")
    public Result<MedicalRecord> getMedicalRecord(){
        // 获取用户健康信息
        MedicalRecord medicalRecord = aiChatService.getMedicalRecord();
        return Result.success(medicalRecord, SUCCESS);
    }

    /**
     * 发送消息到AI
     * @return
     */
    @PostMapping ("/sendMessage")
    public Result<?> sendMessageToAi(@RequestBody AiChatDTO aiChatDTO) throws NoApiKeyException, InputRequiredException, JsonProcessingException {
        // 赋默认值
        if (aiChatDTO.getInputType()==null || aiChatDTO.getInputType().isEmpty()){
            aiChatDTO.setInputType("dialogBox");
        }
        // 判断inputType是否为recommend 是则执行每日推荐
        if (aiChatDTO.getInputType().equals("recommend")){
            return Result.success(aiChatService.aiDailyRecommend(), SUCCESS);
        } else if (aiChatDTO.getInputType().equals("health")){
            // 健康咨询
            AiChatVO aiChatVO = aiChatService.aiHealthInquiry(aiChatDTO);
            return Result.success(aiChatVO, SUCCESS);
        } else if (aiChatDTO.getInputType().equals("dialogBox")){
            // 聊天框对话
            AiChatVO aiChatVO = aiChatService.aiDialogBoxChat(aiChatDTO);
            return Result.success(aiChatVO, SUCCESS);
        } else {
            return Result.error("输入类型错误");
        }
    }
}
